package com.xana.acg.mikomiko.actis.anime;

import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.xana.acg.fac.model.anime.Detail;
import com.xana.acg.mikomiko.App;
import com.xana.acg.com.app.TabViewPagerActivity;
import com.xana.acg.mikomiko.R;
import com.xana.acg.mikomiko.frags.anime.SrcFragment;
import com.xana.acg.mikomiko.frags.comment.CommentFragment;
import com.xana.acg.mikomiko.media.AnimeView;
import com.xana.acg.mikomiko.media.EpisodePopup;
import com.xana.acg.mikomiko.media.JZMediaAliyun;
import com.xana.acg.mikomiko.media.VideoSpeedPopup;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;

@SuppressWarnings("ALL")
public class AnimePlayerActivity extends TabViewPagerActivity
        implements
        VideoSpeedPopup.SpeedChangeListener,
        AnimeView.JzVideoListener,
        EpisodePopup.OnEpisodeClickListener{

    @BindView(R.id.animeView)
    AnimeView animeView;
    @BindView(R.id.edit_danmaku)
    EditText eDanmaku;
    @BindView(R.id.viewStub)
    ViewStub mView;
    private OnFragListener mListener;

    //倍数弹窗
    private VideoSpeedPopup videoSpeedPopup;
    private EpisodePopup episodePopup;

    @OnClick(R.id.iv_trigger)
        void click(View view) {
        App.hintKb(view);
        eDanmaku.setText("");
    }

    @Override
    protected void setFrags() {
        mFrags = Arrays.asList(
                new SrcFragment(),
                new CommentFragment(uri.substring(uri.lastIndexOf('/') + 1)));
    }

    @Override
    protected void setTitles() {
        mTitles = Arrays.asList("简介", "评论");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_anime_player;
    }

    private String uri;

    @Override
    protected boolean initArgs(Bundle bundle) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
        App.getMediaPlayer().pause();
        uri = bundle.getString("uri");
        return uri != null;
    }

    String url = "https://1251316161.vod2.myqcloud.com/29fe1275vodbj1251316161/5c1534725285890816229305794/EsvrHBrCk7kA.mp4";
    String url1 = "https://1251316161.vod2.myqcloud.com/007a649dvodcq1251316161/91a28fd65285890814081538994/N0P7oujHVwYA.mp4";
    String url2 = "http://112.74.191.65:3001/anime/6b313038307c3537393633/0/0/url";
    String url3 = "http://112.74.191.65:3001/proxy/anime/6b313038307c3537393633/0/0";

    @Override
    protected void initWidget() {
        super.initWidget();
        animeView.setJzVideoListener(this);
        animeView.posterImageView.setImageResource(R.drawable.bg_ekidora);
        episodePopup = new EpisodePopup(this);
        episodePopup.setmEcListener(this);
    }
    private JZDataSource mJzDataSource;

    @Override
    protected void initData() {
        super.initData();
        mListener = (OnFragListener) mFrags.get(mVp2.getCurrentItem());
        mListener.onLoad(uri);
     //   setUri(url1, "回复术士");
    }

    boolean init = true;

    public void setUri(String uri, String title) {
//        uri = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
        mJzDataSource = new JZDataSource(uri, title);
        if(init){
            animeView.setUp(mJzDataSource, Jzvd.SCREEN_NORMAL);
            animeView.startVideo();
            animeView.startVideoAfterPreloading();
            init = false;
            return;
        }
        animeView.changeUrl(mJzDataSource, 0);
    }

    @Override
    public void nextClick() {
        mListener.next();
    }

    @Override
    public void backClick() {
        onBackPressed();
    }

    @Override
    public void throwingScreenClick() {

    }

    public void onEpiClick(List<Detail.Eposide> list, int i){
        setUri(list.get(i).getInfo()+"/url", list.get(i).getName());
        episodePopup.setDataList(list);
        episodePopup.play(i);
    }

    @Override
    public void selectPartsClick() {
//        videoEpisodePopup.setPlayNum(episodes.getSelectedTabPosition() + 1);
        episodePopup.showAtLocation(getWindow().getDecorView(), Gravity.RIGHT, 0, 0);
    }

    @Override
    public void speedClick() {
        if (videoSpeedPopup == null) {
            videoSpeedPopup = new VideoSpeedPopup(this);
            videoSpeedPopup.setSpeedChangeListener(this);
        }
        videoSpeedPopup.showAtLocation(getWindow().getDecorView(), Gravity.RIGHT, 0, 0);
    }

    @Override
    public void onBackPressed() {
        if (animeView.backPress()) {
            if (videoSpeedPopup != null)
                videoSpeedPopup.dismiss();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AnimeView.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Jzvd.goOnPlayOnResume();
    }

    @Override
    public void speedChange(float speed) {
        changeSpeed(speed);
    }

    /**
     * 改变播放倍速
     * @param speed
     */
    private void changeSpeed(float speed) {
        animeView.mediaInterface.setSpeed(speed);
        showToast("正在以" + speed + "X倍速播放");
        animeView.speedChange(speed);
    }

    @Override
    public void click(int i) {
        if(mListener!=null)
            mListener.play(i);
    }

    public interface OnFragListener {
        void onLoad(String uri);
        void play(int i);
        void next();
    }
}

